/**
 * Created by lianchao.deng on 2018/3/29.
 */
import {
    ADD_CHATLIST,
    REMOVE_CHATLIST,
    SAVE_CHATLIST,
    GET_CHATLIST,
    SAVE_SEX,
    GET_SEX,
    SAVE_START,
    GET_START
} from './mutation-types.js'

import {setStore, getStore} from '../config/mUtils';

export default {
    //我们可以使用ES2015风格的计算属性命名功能来使用一个常量作为函数名
    // 加入购物车
    [ADD_CHATLIST](state, {
        priority,
        contentA,
        contentB,
        scenarioName, //场景名称
        sceneIndex, //场景编号
        chatIndex //对话编号
    }) {
        if (state.chatArr[sceneIndex]) {
            let chatDialog = {};
            chatDialog.priority = priority;
            chatDialog.contentA = contentA;
            chatDialog.contentB = contentB;
            chatDialog.chatIndex = chatIndex;
            state.chatArr[sceneIndex].chatContent.push(chatDialog);
        } else {
            let chatPart = {};
            let chatDialog = {};
            chatPart.sceneIndex = sceneIndex;
            chatPart.scenarioName = scenarioName;
            chatDialog.priority = priority;
            chatDialog.contentA = contentA;
            chatDialog.contentB = contentB;
            chatDialog.chatIndex = chatIndex;
            chatPart.chatContent = [];
            chatPart.chatContent.push(chatDialog);
            state.chatArr.push(chatPart);
        }

    },
    [REMOVE_CHATLIST] (state, {sceneIndex, type}) {
        if (state.chatArr[sceneIndex]) {
            if (type == 'prev') {
                state.chatArr.splice(sceneIndex, 1);
                state.chatArr.splice(sceneIndex - 1, 1);
            } else {
                state.chatArr.splice(sceneIndex, 1);
            }
        }
    },
    [SAVE_CHATLIST] (state, partid) {
        let allChat = getStore('allChat');
        if(allChat) {
            allChat = JSON.parse(allChat)
        }else {
            allChat = {};
        }
        allChat[partid] = state.chatArr;
        setStore('allChat', allChat);
        state.chatArr = [];
    },
    [GET_CHATLIST] (state, partid) {
        let allChat = getStore('allChat');
        if(allChat) {
            allChat = JSON.parse(allChat);
            if(allChat[partid]) {
                state.partChat = allChat[partid];
            }
        }
    },
    [SAVE_SEX] (state, sex) {
        if(sex) {
            setStore("sex", sex);
        }
    },
    [GET_SEX] (state) {
        let sex = getStore("sex");
        if(sex) {
            state.sex = sex;
        }
    },
    [SAVE_START] (state, start) {
        if(start) {
            state.start = start;
            setStore("start", start);
        }
    },
    [GET_START] (state) {
        let start = getStore("start");
        if(start) {
            state.start = start;
        }
    }


}
